import { h, render } from 'vue'
import MConfirm from './m-confirm.vue'

const renderCom = (options, resolve, reject) => {
  const divEl = document.createElement('div')
  divEl.className = `m-confirm__wrapper`
  // 创建虚拟dom
  const vDom = h(MConfirm, {
    ...options,
    resolveFun: resolve,
    rejectFun: reject
  })
  // 将虚拟DOM挂载到div上
  render(vDom, divEl)
  // 将div添加到body上
  document.body.appendChild(divEl)
}

export const closeBox = () => {
  const confirmBox = document.getElementsByClassName('m-confirm__wrapper')
  confirmBox[0].remove()
}

export default (options) => {
  return new Promise((resolve, reject) => {
    renderCom(
      options,
      () => {
        resolve()
        closeBox()
      },
      reject
    )
  })
}
